package org.sith.taskrunner.gwt.server.services.taskrunner;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.sith.taskrunner.api.toolkit.TaskToolkit;
import org.sith.taskrunner.api.user.Role;
import org.sith.taskrunner.api.user.User;
import org.sith.taskrunner.ejb.db.entity.UserEntity;
import org.sith.taskrunner.gwt.server.ProxyConverter;
import org.sith.taskrunner.ejb.UserThreadKeeper;
import org.sith.taskrunner.ejb.services.TaskToolkitRegisterService;
import org.sith.taskrunner.ejb.services.UserManagerService;
import org.sith.taskrunner.gwt.client.services.taskrunner.LoadInitDataService;

import org.sith.taskrunner.gwt.server.DBEntityTargetChecker;
import org.sith.taskrunner.gwt.server.ProxyTargetChecker;
import org.sith.taskrunner.gwt.shared.proxy.TaskToolkitEntityProxy;
import org.sith.taskrunner.gwt.shared.proxy.UserEntityProxy;

import javax.ejb.EJB;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class LoadInitDataServiceImpl extends RemoteServiceServlet implements LoadInitDataService {

    private final ProxyConverter proxyConverter = new ProxyConverter();

    @EJB(name = "UserManagerServiceEJB")
    private UserManagerService userManagerService;

    @EJB(name = "TaskToolkitRegisterServiceEJB")
    private TaskToolkitRegisterService taskToolkitRegisterService;


    private final DBEntityTargetChecker targetChecker = new DBEntityTargetChecker();
    private final ProxyTargetChecker proxyChecker = new ProxyTargetChecker();


    @Override
    public UserEntityProxy loadCurrentUser() {

        UserEntityProxy userProxy = new UserEntityProxy();


        proxyConverter.convert(UserThreadKeeper.getThreadUser(), userProxy, targetChecker);


        return userProxy;
    }

    @Override
    public List<TaskToolkitEntityProxy> loadSystemToolkits() {

        taskToolkitRegisterService.getSystemToolkits();

        return null;
    }

    @Override
    public List<UserEntityProxy> loadUsers() {

        List<? extends User> allUsers = userManagerService.getAllUsers();
        List<UserEntityProxy> userProxies = new ArrayList<UserEntityProxy>();
        for (User user : allUsers) {
            UserEntityProxy userEntityProxy = new UserEntityProxy();
            proxyConverter.convert(user, userEntityProxy, targetChecker);
            userProxies.add(userEntityProxy);
        }

        return userProxies;
    }

    @Override
    public List<TaskToolkitEntityProxy> loadToolkits(UserEntityProxy currentUser) {
        UserEntity user = new UserEntity();
        proxyConverter.convert(currentUser, user,targetChecker);
        List<TaskToolkit> toolkits;
        if (user.getRole() == Role.USER) {
            toolkits = taskToolkitRegisterService.getActiveToolkits();
        } else {
            toolkits = taskToolkitRegisterService.getAllToolkits();
        }


        List<TaskToolkitEntityProxy> proxies = new ArrayList<TaskToolkitEntityProxy>();
        for (TaskToolkit toolkit : toolkits) {
            TaskToolkitEntityProxy proxy = new TaskToolkitEntityProxy();

            proxyConverter.convert(toolkit, proxy,targetChecker);
            proxies.add(proxy);
        }

        return proxies;
    }

}